package com.wuxuan.javabase_homework.homework3;

import java.util.List;
import java.util.Scanner;

/**
 * 客户端进入店铺的界面
 */
public class ShopScreen {
    private static Scanner sc = new Scanner(System.in);
    private Shop shop;
    private List<Book> books;
    //传入当前用户
    private People user;

    ShopScreen(Shop shop, People user) {
        this.shop = shop;
        this.books = shop.getBooks();
        this.user = user;
    }

    /**
     * 进入店铺启动按钮
     */
    public void start() {
        menu();
    }

    /**
     * 店铺界面的界面
     */
    private void menu() {
        while (true) {
            System.out.println("======================店铺：" + shop.getShopName() + "========================");
            System.out.println("0.返回");
            System.out.println("1.查看所有书籍(按销售量从高到低排序)");
            System.out.println("2.购买书籍");
            int choose = sc.nextInt();
            switch (choose) {
                case 0:
                    return;
                case 1:
                    selectAllBooks();
                    break;
                case 2:
                    buyBooks();
                    break;
                default:
                    System.out.println("请输入正确的序号！");
            }
        }
    }

    /**
     * 查看所有书籍
     */
    private void selectAllBooks() {
        System.out.println("======================查看所有书籍========================");
        if (books.isEmpty()) {
            System.out.println("当前店铺还没有任何书籍噢~");
        } else {
            //先按销售量从高到低排序
            books.sort((o1, o2) -> Integer.compare(o2.getSale(), o1.getSale()));
            //然后再遍历
            for (Book book : books) {
                System.out.println(book);
            }
        }
    }


    /**
     * 购买书籍
     */
    private void buyBooks() {
        while (true) {
            System.out.println("======================购买书籍========================");
            if (books.isEmpty()) {
                System.out.println("当前店铺还没有任何书籍噢~");
            } else {
                double balance = user.getBalance();
                System.out.println("您当前余额：" + balance);

                System.out.println("请输入您想购买的书籍ID(输入-1返回)：");
                int id = sc.nextInt();

                if (id == -1) {
                    return;
                }

                boolean exist = books.stream().anyMatch(book -> book.getId() == id);

                if (exist) {
                    Book book = books.stream().filter(b -> b.getId() == id).findFirst().get();
                    //获取书籍信息
                    double bPrice = book.getActualPrice();
                    int inventory = book.getInventory();
                    int sales = book.getSale();

                    System.out.println("请输入您想购买的书籍数量：");
                    int quantity = sc.nextInt();

                    double totalPrice = bPrice * quantity;

                    //看库存
                    if (quantity > inventory) {
                        System.out.println("此书的剩余库存：" + inventory + ",您购买超出数量！");
                        continue;
                    }

                    //看余额
                    if (totalPrice > balance) {
                        System.out.println("此次购买书籍实际单价：" + bPrice + ",最终总价：" + totalPrice + ",您的余额不足！");
                        continue;
                    }

                    //思：过滤没问题之后,开始交易(如果有数据库的话，这里整个过程传数据库连接，然后手动事务，保持整个过程没问题，出问题回滚比较好！)
                    //减去用户余额
                    user.setBalance(balance - totalPrice);
                    //增加商家余额(这里设计的还是比较简单，一般还是加在店铺的总销售额上？然后店铺有很多员工去分配这些...?之后用框架开发项目的时候再具体化，这里注重练习基础我就简化了)
                    shop.getMerchant().setBalance(shop.getMerchant().getBalance() + totalPrice);
                    //减去书籍库存
                    book.setInventory(inventory - quantity);
                    //增加书籍销售量
                    book.setSale(sales + quantity);

                    System.out.println("购买成功~");


                }

            }

        }
    }
}





